package br.com.californiahotel.server.serviceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import br.com.californiahotel.client.service.LocacaoService;
import br.com.californiahotel.server.hibernate.DatabaseUtils;
import br.com.californiahotel.server.hibernate.models.Apartamento;
import br.com.californiahotel.server.hibernate.models.Hospede;
import br.com.californiahotel.server.hibernate.models.Locacao;
import br.com.californiahotel.server.utils.Conversor;
import br.com.californiahotel.shared.models.Servico;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class LocacaoServiceImpl extends RemoteServiceServlet implements LocacaoService {

	private static final Class<Locacao> RESERVA_CLASS = br.com.californiahotel.server.hibernate.models.Locacao.class;
	private static final DatabaseUtils<Locacao> DATABASE = new DatabaseUtils<Locacao>();
	private static final Conversor CONVERSOR = new Conversor();

	@Override
	public boolean save(br.com.californiahotel.shared.models.Locacao model) {
		try {
			Locacao locacao =  new Locacao();
			locacao.setData(new Date());
			locacao.setApartamento(new DatabaseUtils<Apartamento>().find(Apartamento.class, Integer.parseInt(model.getApartamento())));
			locacao.setHospede(new DatabaseUtils<Hospede>().executeQuery(Hospede.class,"SELECT * FROM hospede h where h.nome like ?","%"+model.getHospede()+"%").get(0));
			DATABASE.save(locacao);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean edit(br.com.californiahotel.shared.models.Locacao model, String id) {
		try {
			DATABASE.removeById(RESERVA_CLASS, id);
			DATABASE.save((Locacao)CONVERSOR.convertModelToHibernate( model, RESERVA_CLASS));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public List<br.com.californiahotel.shared.models.Locacao> findAll() {
		ArrayList<br.com.californiahotel.shared.models.Locacao> LocacaoList = new ArrayList<br.com.californiahotel.shared.models.Locacao>();
		List<Locacao> findAll = DATABASE.findAll(RESERVA_CLASS);
		for (Locacao Locacao : findAll) {
			LocacaoList.add(CONVERSOR.convertLocacaoHibernateToLocacaoModel(Locacao));
		}
		return LocacaoList;
	}

	@Override
	public boolean delete(String key) {
		try {
			DATABASE.removeById(RESERVA_CLASS, Long.parseLong(key));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public List<Servico> findAllServicosById(Integer idApto) {
		List<Servico> servicoList = new ArrayList<Servico>();
		List<Long> idServicos = new DatabaseUtils().executeQuery("SELECT ls.servicos_cdServico FROM locacao_servico ls where ls.Locacao_id=?", idApto);
		for (Long id : idServicos) {
			br.com.californiahotel.server.hibernate.models.Servico ServiceFinded = new DatabaseUtils<br.com.californiahotel.server.hibernate.models.Servico>().find(br.com.californiahotel.server.hibernate.models.Servico.class, id);
			servicoList.add((Servico) CONVERSOR.convertHibernateToModel(ServiceFinded, Servico.class));
		}
		return servicoList;
	}

}
